/**
 * 
 */
package com.furschina.struts2.portal;

import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import cn.wooshai.core.struts2.PaginationAware;
import cn.wooshai.core.struts2.WooshaiAction;
import cn.wooshai.core.vo.PageModel;

import com.furschina.Constants;
import com.furschina.model.Channel;
import com.furschina.service.ChannelService;
import com.furschina.struts2.BaseAction;
import com.furschina.vo.ChannelPage;

/**
 * @author iday
 * 
 */
@Controller("portal/channel")
@InterceptorRefs({ @InterceptorRef(Constants.DEFAULT_STACK_REF),
		@InterceptorRef(Constants.VALIDATOR_REF) })
@Result(name = WooshaiAction.SUCCESS, type = "redirect", location = "/portal/channel.xhtml")
public class ChannelAction extends BaseAction implements PaginationAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7896454681680197952L;

	static Log LOG = LogFactory.getLog(ChannelAction.class);
	private Channel model;
	private ChannelPage pm = new ChannelPage();
	private Collection<Channel> list;
	private String id;
	private ChannelService channelService;

	/**
	 * @param model
	 *            the model to set
	 */
	public void setModel(Channel model) {
		this.model = model;
	}

	/**
	 * @return the model
	 */
	public Channel getModel() {
		return model;
	}

	@Override
	public void setPage(int i) {
		this.pm.setPage(i);

	}

	@Override
	public PageModel getPm() {
		return this.pm;
	}

	/**
	 * @param list
	 *            the list to set
	 */
	public void setList(Collection<Channel> list) {
		this.list = list;
	}

	/**
	 * @return the list
	 */
	public Collection<Channel> getList() {
		return list;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param channelService
	 *            the channelService to set
	 */
	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}

	/**
	 * @return the channelService
	 */
	public ChannelService getChannelService() {
		return channelService;
	}

	/*
	 * ================================================
	 */
	public String index() {
		list = channelService.findAll(pm);
		return INDEX;
	}

	public String show() {
		this.model = this.channelService.findByPrimaryKey(id);
		return SHOW;
	}

	public String create() {
		if(!list.isEmpty()) {
			this.channelService.createOrUpdateAll(list);
		}
		return SUCCESS;
	}

	public String update() {
		this.channelService.update(model);
		return SUCCESS;
	}

	public String include() {
		this.list = channelService.findAll(pm);
		return null;
	}
}
